<template>
	<view>
		<view class="head">
			<view class=""></view>
			<view class="right" @click="toPage('/sub-personal/gallery-ai/gallery-ai')">
				<image src="../../static/images/icon/my-nav3.png" class="nav"></image>
				<view class="">我的绘画</view>
				<image src="../../static/images/icon/right.png" class="nav"></image>
			</view>
		</view>
		<view class="item">
			<view class="title">
				<view>英文tag</view>
			</view>
			<uni-easyinput :disabled="!open" v-model="goodStr" type="textarea" maxlength="80" placeholder="请输入" />
		</view>
		<view class="re-box">
			<view class="submit" @click="reStr">重置</view>
		</view>
		<view class="btn">
			<view class="submit" @click="toPage('/sub-diy/tag/tag')">去选择Tag</view>
			<view class="submit" @click="submit" v-if="show">AI绘图提交</view>
			<view class="submit" @click="toDetail">查看绘图</view>
		</view>
		<view class="img-box">
			<image v-if="img" :src="img" class="img"></image>
			<image v-else :src="imgCover"></image>
		</view>
	</view>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				imgCover: 'https://img14.360buyimg.com/n1/jfs/t1/126958/11/24724/48501/62cf7c09E3e21543c/be03f2fe9e07c945.jpg.avif',
				img: '',
				imgh: 800,
				imgw: 600,
				goodStr: '',
				badStr: 'nsfw, lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry',
				_id: '',
				show: true,
				open: null,

			}
		},
		computed: {},
		watch: {},
		methods: {
			toPage(e) {
				uni.navigateTo({
					url: e
				})
			},
			toDetail() {
				if (this._id) {
					uni.navigateTo({
						url: '/sub-diy/aiDetail/aiDetail?_id=' + this._id
					})
				} else {
					uni.showToast({
						icon: "none",
						title: "未提交绘图"
					})
				}
			},
			async getOpen() {
				let db = wx.cloud.database()
				let info = await db.collection('config').where({
					name: "config"
				}).get()
				// console.log()
				this.open = info.data[0].open
				// this.info = info.data[0]
			},
			async submit() {
				if (!this.goodStr) {
					uni.showToast({
						icon: "none",
						title: "输入关键词"
					})
					return
				}
				let goodStr = "masterpiece, best quality," + this.goodStr
				//审查文字
				let res = await wx.cloud.callFunction({
					name: "checkMsg",
					data: {
						msg: this.goodStr
					}
				})
				if (res.result.result.label != 100) {
					uni.showToast({
						icon: "none",
						title: "关键词不通过"
					})
					return
				}

				this.show = false
				setTimeout(() => {
					this.show = true
				}, 60000)

				uni.showLoading({
					title: '提交渲染信息！'
				})
				//生成订单
				let orderRes = await wx.cloud.callFunction({
					name: 'orderAdd',
					data: {
						user_id: uni.getStorageSync('user_id'),
						data: {
							data: [
								goodStr,
							]
						}
					}
				})
				this._id = orderRes.result.data._id
				uni.hideLoading()
				uni.showToast({
					icon: "none",
					title: "提交成功，请点击查看绘图"
				})


				// 发送绘图
				let aiRes = await wx.cloud.callFunction({
					name: 'sendAi',
					// name: 'sendAiCon',
					data: {
						_id: orderRes.result.data._id,
						user_id: uni.getStorageSync('user_id'),
						data: {
							data: [
								goodStr,
								"nsfw, lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry",
								"None",
								"None",
								28,
								"Euler a",
								false,
								false,
								1,
								1,
								11,
								-1,
								-1,
								0,
								0,
								0,
								false,
								768,
								512,
								false,
								0.7,
								0,
								0,
								"None",
								0.9,
								5,
								"0.0001",
								false,
								"None",
								"",
								0.1,
								false,
								false,
								false,
								null,
								"",
								"Seed",
								"",
								"Nothing",
								"",
								true,
								false,
								false,
								null,
								"",
								"",
							]
						}
					}
				})

				console.log(aiRes)

			},

			save() {
				if (!this.img) {
					uni.showToast({
						icon: "none",
						title: "没有绘图"
					})
					return
				}
				uni.getSetting({ //获取用户的当前设置
					success: (res) => {
						console.log("获取权限", res);
						if (res.authSetting['scope.writePhotosAlbum']) { //验证用户是否授权可以访问相册
							this.getSave()
						} else {
							uni.authorize({ //如果没有授权，向用户发起请求
								scope: 'scope.writePhotosAlbum',
								success: () => {
									this.getSave()
								},
								fail: () => {
									uni.showToast({
										title: "请打开保存相册权限，再点击保存相册分享",
										icon: "none",
										duration: 3000
									});
									setTimeout(() => {
										uni.openSetting({ //调起客户端小程序设置界面,让用户开启访问相册
											success: (res2) => {
												// console.log(res2.authSetting)
											}
										});
									}, 3000);
								}
							})
						}
					}
				})

			},
			getSave() {
				let img = this.img
				let filePath = wx.env.USER_DATA_PATH + '/ai' + new Date().getTime() + '.png'
				wx.getFileSystemManager().writeFile({
					filePath: filePath,
					data: img.slice(22), //注意这里
					encoding: 'base64',
					success: res => {
						console.log('success')
						wx.saveImageToPhotosAlbum({
							filePath: filePath, //这是把临时文件 保存到 相册, 收工
							success: res => {
								wx.showToast({
									title: '保存成功！'
								})
							},
							fail: error => {
								console.log(error)
							}
						})
					},
					fail: error => {
						console.log(error)
					}
				})
			},
			setStr() {
				let str = uni.getStorageSync('tagStr')
				if (str) {
					this.goodStr = str
					uni.removeStorageSync('tagStr')
				}
			},
			reStr() {
				this.goodStr = ''
			},
		},
		onLoad(options) {
			this.getOpen()
		},
		onShow() {
			this.setStr()
		},
		onReady() {},
		onPullDownRefresh() {},
		onReachBottom() {},
		onShareAppMessage() {},
		onPageScroll() {},

	}
</script>

<style lang="scss" scoped>
	.head {
		background-color: #fff;
		display: flex;
		justify-content: space-between;
		height: 80rpx;
		padding-right: 30rpx;

		.right {
			display: flex;
			align-items: center;
			font-size: 28rpx;

			.nav {
				width: 36rpx;
				height: 36rpx;
				margin-right: 10rpx;
			}
		}
	}

	.item {
		display: flex;
		align-items: center;
		background-color: #fff;
		padding-top: 30rpx;
		padding-bottom: 30rpx;
		padding-right: 30rpx;

		/deep/ textarea {
			color: #000 !important;
		}

		.title {
			width: 160rpx;
			font-size: 30rpx;
			margin-left: 30rpx;
			margin-right: 20rpx;
		}
	}

	.img-box {
		text-align: center;
		background-color: #fff;
		padding-bottom: 50rpx;

		.img {
			width: 600rpx;
			height: 900rpx;
		}
	}

	.btn {
		background-color: #fff;
		padding-bottom: 80rpx;

		.submit {
			margin: 0 auto;
			margin-bottom: 20rpx;
			width: 200rpx;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background: linear-gradient(#00AEEC, #1296db);
			color: #fff;
			border-radius: 20rpx;
		}
	}

	.re-box {
		height: 60rpx;
		background-color: #fff;
		position: relative;

		.submit {
			position: absolute;
			top: -10rpx;
			right: 30rpx;
			font-size: 26rpx;
			width: 120rpx;
			height: 60rpx;
			line-height: 60rpx;
			text-align: center;
			background: linear-gradient(#00AEEC, #1296db);
			color: #fff;
			border-radius: 20rpx;
		}
	}
</style>
